<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var rob = function (nums) {
      const len = nums.length
      if (len == 0) return 0
      const dp = new Array(len + 1)
      dp[0] = 0
      dp[1] = nums[0]
      for (var i = 2; i <= len; i++) {
        dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i - 1])
      }
      return dp[len]
    };
    console.log(rob([1, 2, 3, 1]))
  </script>
</body>

</html>